import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutterdouban/model/homeModel/home_movelist_model.dart';
import 'package:flutterdouban/widgets/dashed_line.dart';
import 'package:flutterdouban/widgets/star_rating.dart';

class JKHomeMovieItem extends StatelessWidget {

  final MovieItem movieModel;

  JKHomeMovieItem(this.movieModel);

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.all(8),
      decoration: BoxDecoration(
        border: Border(
          bottom: BorderSide(width: 8, color: Colors.green)
        )
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          buildHeader(),
          SizedBox(height: 8,),
          buildContent(),
          SizedBox(height: 8,),
          buildBottomFooter()
        ],
      ),
    );
  }

  /* 1、头部布局 */
  Widget buildHeader() {
    return Container(
      padding: EdgeInsets.fromLTRB(10, 5, 10, 5),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(6),
        color: Color.fromARGB(255, 238, 205, 144)
      ),
      child: Text(
        'No.${movieModel.rank}',
        style: TextStyle(fontSize: 18, color: Color.fromARGB(255, 131, 95, 36)),
      ),
    );
  }

  /* 2、内容的布局 */
  Widget buildContent() {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        buildContentImage(),
        SizedBox(width: 8,),
        buildContentInfo(),
        SizedBox(width: 8,),
        buildContentLine(),
        SizedBox(width: 8,),
        buildContentWish(),
        SizedBox(width: 8,)
      ],
    );
  }
  // 2.1、内容的图片
  Widget buildContentImage() {
    return ClipRRect(
      borderRadius: BorderRadius.circular(8),
      child: FadeInImage.assetNetwork(
        placeholder: 'assets/images/placeholder.png',
        image: movieModel.imageURL,
        width: 150,
        fit: BoxFit.fitWidth,
      )
    );
  }

  // 2.2、内容的信息
  Widget buildContentInfo() {
    // Expanded 代表可伸缩，到时候文本可换行
    return Expanded(
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          buildContentInfoTitle(),
          SizedBox(height: 8,),
          buildContentInfoRate(),
          buildContentInfoDesc()
        ],
      ),
    );
  }
  // 2.2.1、图片、名字、电影上映日期
  Widget buildContentInfoTitle() {
    return Text.rich(
      TextSpan(
        children: [
          WidgetSpan(
            child: Icon(Icons.play_circle_outline, color: Colors.red, size: 24,),
            alignment: PlaceholderAlignment.middle
          ),
          WidgetSpan(
              child: Text(movieModel.title, style: TextStyle(fontSize: 20, color: Colors.black)),
              alignment: PlaceholderAlignment.middle
          ),
          WidgetSpan(
              child: Text('(${movieModel.playDate})', style: TextStyle(fontSize: 18, color: Colors.black)),
              alignment: PlaceholderAlignment.middle
          ),
        ]
      )
    );
  }
  // 2.2.2、评分
  Widget buildContentInfoRate() {
    return FittedBox(
      child: Row(
        children: <Widget>[
          JKStarRating(rating: movieModel.rating, starSize: 20,),
          Text('${movieModel.rating}', style: TextStyle(fontSize: 20, color: Colors.brown),)
        ],
      ),
    );
  }
  // 2.2.3、描述
  Widget buildContentInfoDesc() {
    // 字符串的拼接
    // 1、电影类型
    final genresString = movieModel.genres.join(' ');
    // 2、导演
    final directorString = movieModel.director.name;
    // 3、演员
    final actorString = movieModel.casts.map((item) => item.name).toList().join(' ');

    return Text(
        '$genresString / $directorString / $actorString',
      maxLines: 2,
      // 超出剪切...显示
      overflow: TextOverflow.ellipsis,
    );
  }

  /* 2.3、右边垂直的线 */
  Widget buildContentLine() {
    return Container(
      height: 120,
      child: JKDashedLine(
        axis: Axis.vertical,
        dashedCount: 12,
        dashedWidth: 0.5,
        dashedHeight: 6,
        dashedColor: Colors.grey,
      ),
    );
  }

  /* 2.4、想看 */
  Widget buildContentWish() {
    return Container(
      height: 120,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Image.asset('assets/images/home/wish.png', width: 36, height: 36,),
          Text('想看', style: TextStyle(fontSize: 15, color: Color.fromARGB(255, 235, 170, 60)),)
        ],
      ),
    );
  }

  /* 3、底部的文字 */
  Widget buildBottomFooter() {
    return Container(
      // 无限延伸
      width: double.infinity,
      padding: EdgeInsets.all(8),
      decoration: BoxDecoration(
        color: Color(0xffe2e2e2),
        borderRadius: BorderRadius.circular(6)
      ),
      child: Text(
        movieModel.originalTitle,
        style: TextStyle(fontSize: 16, color: Color(0xff666666)),
      ),
    );
  }

}

//
//Text(movieModel.title, style: TextStyle(fontSize: 18, color: Colors.black),),
//Text('(${movieModel.playDate})', style: TextStyle(fontSize: 16, color: Colors.grey),),